package com.educhains.module.blog.service.impl;

import com.educhains.module.blog.persist.mybatis.BlogReplyMapper;
import com.educhains.module.blog.service.BlogService;
import com.educhains.module.blog.vo.BlogContent;
import com.educhains.module.blog.vo.BlogReply;
import com.educhains.module.demo.vo.Page;
import com.educhains.module.blog.persist.mybatis.BlogMapper;
import com.educhains.module.blog.vo.Blog;
import com.educhains.web.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Tengda Tang on 2017/8/18.
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper = null;

    @Autowired
    private BlogReplyMapper blogReplyMapper = null;


    public Page showByPage(int pageNum, int option) {

        //一页能容纳的最多文章数量
        List list;
        int totalRecord = blogMapper.getAllBlog().size();
        Page page = new Page(pageNum, totalRecord);
        switch (option){
            case 1:
                list = blogMapper.showByPage(page.getStartIndex(), page.getPageSize());
                break;
            case 2:
                list = blogMapper.showByPageByLikes(page.getStartIndex(), page.getPageSize());
                break;
            case 3:
                list = blogMapper.showByPageByReads(page.getStartIndex(), page.getPageSize());
                break;
            default:
                list = blogMapper.showByPage(page.getStartIndex(), page.getPageSize());
        }

        page.setList(list);
        return page;
    }

    public void addReadNum(String blogId){
        blogMapper.addReadNum(blogId);
    }

    public void addLike(String blogId){
        blogMapper.addLike(blogId);
    }

    public void subtractLike(String blogId){
        blogMapper.subtractLike(blogId);
    }

    public void addReplyNum(String blogId){
        blogReplyMapper.addReplyNum(blogId);
    }

    public Blog findBlog(String blogId) {return blogMapper.findBlog(blogId);}

    public BlogContent findBlogContent(String blogId){

        return blogMapper.findBlogContent(blogId);
    }

    public Page replyShowByPage(int pageNum, String blogId) {

        //一页能容纳的最多文章数量
        int totalRecord = blogReplyMapper.getAllReplies(blogId).size();
        Page page = new Page(pageNum, totalRecord);
        List list = blogReplyMapper.showByPage(page.getStartIndex(), page.getPageSize(), blogId);
        page.setList(list);
        return page;
    }


    public void addBlog(String blogId, String blogName, String userId, String summary, int isOriginal, String blogContent){

        blogMapper.addBlog(blogId, blogName, userId, summary, isOriginal, blogContent);

    }


    public void addReply(String replyId, String blogId ,String userId, String content){

        blogReplyMapper.addReply(replyId ,blogId , userId, content);

    }

    public List<BlogReply> getReply(String blogId, String locationId){ return blogReplyMapper.getReply(blogId, locationId); }

    public void submitInFloorReply(String replyId, String userId, String blogId, String locationId, String content){

        blogReplyMapper.addReplyNum(locationId);
        blogReplyMapper.submitInFloorReply(replyId, userId, blogId, locationId, content);
    }

    public List<Blog> getBlogListByUserIdWithLimit(String userId, int limit) {
        return blogMapper.getBlogListByUserIdWithLimit(userId, limit);
    }

    public List<BlogReply> getBlogReplyListByUserIdWithLimit(String userId, int limit) {
        return blogReplyMapper.getBlogReplyListByUserIdWithLimit(userId, limit);
    }

    public List<Blog> selectBlogListLimit(int blogNum, String type) {
        return blogMapper.selectBlogListLimit(blogNum, type);
    }
}
